package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class EsDocumentDome {
    @Autowired
    public IHotelService iHotelService;

    /**
     * 新增文档
     *
     * @throws IOException
     */
    @Test
    public void addTest() throws IOException {
        //从数据库查询一条文档
        Hotel hotel = iHotelService.getById(36934);
        //将hotel转换为HotelDoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //创建请求对象
        IndexRequest request = new IndexRequest("hotel");
        //设置文档id
        request.id(hotelDoc.getId() + "");
        //设置文档内容
        String json = JSON.toJSONString(hotelDoc);
        request.source(json, XContentType.JSON);
        //调用客户端请求
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());
    }

    /**
     * 查询文档
     *
     * @throws IOException
     */
    @Test
    public void getIndex() throws IOException {
        // 准备Request
        GetRequest request = new GetRequest("hotel", "45870");
        //发送请求，得到响应
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        //解析响应结果
        String json = response.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println(hotelDoc);
    }

    /**
     * 修改文档
     *
     * @throws IOException
     */
    @Test
    public void updateIndex() throws IOException {
        // 准备Request
        UpdateRequest request = new UpdateRequest("hotel", "36934");
        //修改字段
        request.doc(
                "price", "950",
                "starName", "四钻"
        );
        //调用客户端请求
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    }

    /**
     * 删除文档
     *
     * @throws IOException
     */
    @Test
    public void deleteIndex() throws IOException {
        //准备Request
        DeleteRequest request = new DeleteRequest("hotel", "36934");
        //调用客户端请求
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
    }

    /**
     * 批量导入文档
     *
     * @throws IOException
     */
    @Test
    public void bulkAddDocument() throws IOException {
        List<Hotel> list = iHotelService.list();
        // 1.创建Request
        BulkRequest request = new BulkRequest();
        for (Hotel hotel : list) {
            //转换文档类型
            HotelDoc hotelDoc = new HotelDoc(hotel);
            //变为JSON格式
            String json = JSON.toJSONString(hotelDoc);
            //创建新增文档的Request对象
            request.add(new IndexRequest("hotel").source(json, XContentType.JSON));
        }
        //调用客户端请求
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        //响应状态（OK成功）
        System.out.println(response.status());
    }

    //全局
    private RestHighLevelClient client;

    //初始化RestHighLevelClient
    @BeforeEach
    public void setup() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://180.76.145.80:9200")
        ));
    }
}
